package com.ygs.secs.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ygs.secs.model.Instructor;
import com.ygs.secs.service.InstructorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 辅导员管理
 */

@Controller
@RequestMapping("/admin")
public class InstructorMangerController {
    @Autowired
    InstructorService instructorService;


    /**
     * 查询所有辅导员，或根据条件查询
     *
     *  @param instructor 辅导员实体条件
     *
     * @return查询结果
     */
    @RequestMapping("/getInstructorByIdAndName")
    @ResponseBody
    public Map<String, Object> getInstructorByIdAndName(Instructor instructor,@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20")int limit) {
        Map<String,Object> instructorData = new HashMap<>();
        try {
            //分页查询
            Page startPage = PageHelper.startPage(page, limit);
            List<Instructor> instructorList = instructorService.getInstructorByIdAndName(instructor);
            if (instructorList.size()>0){
                instructorData.put("code",200);
                instructorData.put("msg","查询指导教师数据成功");
                instructorData.put("data",instructorList);
                instructorData.put("count", startPage.getTotal());
            }else{
                instructorData.put("code",201);
                instructorData.put("msg","数据为null，请先添加");
            }
            return instructorData;
        }catch (Exception e){
            instructorData.put("code", 500);
            instructorData.put("msg", "system error");
            return instructorData;
        }
    }

    /**
     * 根据id删除辅导员信息
     *
     * @param instructorId 辅导员工号ID
     * @return 删除结果
     */
    @RequestMapping(value = "/delInstructorById",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delInstructorById(@RequestParam("instructorId")String instructorId) throws Exception {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = instructorService.deleteByPrimaryKey(instructorId);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }

    /**
     * 修改辅导员信息
     *
     * @param instructor 辅导员实体
     * @return 操作结果
     */
    @RequestMapping(value = "/updateInstructor",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateInstructor(Instructor instructor) {
        Map<String, Object> result = new HashMap<>();
        int res = instructorService.updateByPrimaryKeySelective(instructor);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 添加辅导员信息
     *
     * @param instructor 辅导员实体
     * @return 操作结果
     */
    @RequestMapping(value = "/addInstructor",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addInstructor(Instructor instructor) {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = instructorService.insert(instructor);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 批量修改辅导员信息
     *
     * @param jsonArrayInstructor 辅导员实体json数组串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchUpdateInstructor",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchUpdateInstructor(String jsonArrayInstructor) {
        Map<String, Object> result = new HashMap<>();
        List<Instructor> instructors= JSONArray.parseArray(jsonArrayInstructor,Instructor.class);
        int res = instructorService.updateBatchSelective(instructors);
        if (res > 0) {
            result.put("code", 200);
            result.put("msg", "操作成功");
        } else {
            result.put("code", 500);
            result.put("msg", "操作失败");
        }
        return result;
    }

    /**
     * 批量添加辅导员信息
     *
     * @param jsonArrayInstructor 辅导员数组json串
     * @return 操作结果
     */
    @RequestMapping(value = "/batchInsertInstructor",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchInsertInstructor(String jsonArrayInstructor) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Instructor> instructors= JSONArray.parseArray(jsonArrayInstructor,Instructor.class);
            int res = instructorService.batchInsert(instructors);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 501);
            result.put("msg", "操作失败，主键冲突，请先修改重复ID");
            return result;
        }
    }

    /**
     * 批量删除辅导员信息
     *
     * @param instructors 辅导员id数组
     * @return 操作结果
     */
    @RequestMapping(value = "/batchDelInstructor",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchDelInstructor(String[] instructors) {
        Map<String, Object> result = new HashMap<>();
        try {
            int res = instructorService.batchDelete(instructors);
            if (res > 0) {
                result.put("code", 200);
                result.put("msg", "操作成功");
            } else {
                result.put("code", 500);
                result.put("msg", "操作失败");
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            result.put("code", 500);
            result.put("msg", "操作失败，请先删除所属信息");
            return result;
        }
    }
}
